home *** CD-ROM | disk | FTP | other *** search
/ PC go! 2008 April / PCgo 2008-04 (DVD).iso / interface / contents / demoversionen_3846 / 13664 / files / Data1.cab / mddialog.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2001-10-16  |  24.4 KB  |  966 lines

  1. /******************************************************************/
  2. /*                                                                */
  3. /*                      TurboCAD for Windows                      */
  4. /*                   Copyright (c) 1993 - 2001                    */
  5. /*             International Microcomputer Software, Inc.         */
  6. /*                            (IMSI)                              */
  7. /*                      All rights reserved.                      */
  8. /*                                                                */
  9. /******************************************************************/
  10.  
  11. // MDDialog.cpp : implementation file
  12. //
  13.  
  14. #include "stdafx.h"
  15. #include "MakeDwg.h"
  16. #include "MDDialog.h"
  17.  
  18. #ifdef _DEBUG
  19. #define new DEBUG_NEW
  20. #undef THIS_FILE
  21. static char THIS_FILE[] = __FILE__;
  22. #endif
  23.  
  24. /////////////////////////////////////////////////////////////////////////////
  25. // CAboutDlg dialog used for App About
  26.  
  27. class CAboutDlg : public CDialog
  28. {
  29. public:
  30.     CAboutDlg();
  31.  
  32. // Dialog Data
  33.     //{{AFX_DATA(CAboutDlg)
  34.     enum { IDD = IDD_ABOUTBOX };
  35.     //}}AFX_DATA
  36.  
  37.     // ClassWizard generated virtual function overrides
  38.     //{{AFX_VIRTUAL(CAboutDlg)
  39.     protected:
  40.     virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
  41.     //}}AFX_VIRTUAL
  42.  
  43. // Implementation
  44. protected:
  45.     //{{AFX_MSG(CAboutDlg)
  46.     //}}AFX_MSG
  47.     DECLARE_MESSAGE_MAP()
  48. };
  49.  
  50. CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
  51. {
  52.     //{{AFX_DATA_INIT(CAboutDlg)
  53.     //}}AFX_DATA_INIT
  54. }
  55.  
  56. void CAboutDlg::DoDataExchange(CDataExchange* pDX)
  57. {
  58.     CDialog::DoDataExchange(pDX);
  59.     //{{AFX_DATA_MAP(CAboutDlg)
  60.     //}}AFX_DATA_MAP
  61. }
  62.  
  63. BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
  64.     //{{AFX_MSG_MAP(CAboutDlg)
  65.         // No message handlers
  66.     //}}AFX_MSG_MAP
  67. END_MESSAGE_MAP()
  68.  
  69. /////////////////////////////////////////////////////////////////////////////
  70. // CMakeDwgDlg dialog
  71.  
  72. CMakeDwgDlg::CMakeDwgDlg(CWnd* pParent /*=NULL*/)
  73.     : CDialog(CMakeDwgDlg::IDD, pParent),
  74.     m_pIApplication(NULL) // TurboCAD SDK: initialize
  75. {
  76.     //{{AFX_DATA_INIT(CMakeDwgDlg)
  77.         // NOTE: the ClassWizard will add member initialization here
  78.     //}}AFX_DATA_INIT
  79.     // Note that LoadIcon does not require a subsequent DestroyIcon in Win32
  80.     m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
  81. }
  82.  
  83. // TurboCAD SDK: make sure we release the application
  84. CMakeDwgDlg::~CMakeDwgDlg()
  85. {
  86.     m_view.SetView(NULL);
  87.  
  88.     // Finished with application, decrement the reference count
  89.     if (m_pIApplication != NULL)
  90.         m_pIApplication->Release();
  91. }
  92.  
  93. void CMakeDwgDlg::DoDataExchange(CDataExchange* pDX)
  94. {
  95.     CDialog::DoDataExchange(pDX);
  96.     //{{AFX_DATA_MAP(CMakeDwgDlg)
  97.         // NOTE: the ClassWizard will add DDX and DDV calls here
  98.     //}}AFX_DATA_MAP
  99. }
  100.  
  101. BEGIN_MESSAGE_MAP(CMakeDwgDlg, CDialog)
  102.     //{{AFX_MSG_MAP(CMakeDwgDlg)
  103.     ON_WM_SYSCOMMAND()
  104.     ON_WM_PAINT()
  105.     ON_WM_QUERYDRAGICON()
  106.     ON_BN_CLICKED(IDC_ADDCIRCLE, OnAddCircle)
  107.     ON_BN_CLICKED(IDC_ADDLINE, OnAddLine)
  108.     ON_BN_CLICKED(IDC_ADDSPLINE, OnAddSpline)
  109.     ON_BN_CLICKED(IDC_NEWDRAWING, OnNewDrawing)
  110.     ON_BN_CLICKED(IDC_INPROC_SERVER, OnInprocServer)
  111.     ON_BN_CLICKED(IDC_LOCAL_SERVER, OnLocalServer)
  112.     ON_BN_CLICKED(IDC_OPENDRAWING, OnOpenDrawing)
  113.     ON_BN_CLICKED(IDC_SAVEDRAWING, OnSaveDrawing)
  114.     ON_BN_CLICKED(IDC_GETACTIVE, OnGetActive)
  115.     ON_LBN_SELCHANGE(IDC_LISTDRAWINGS, OnSelchangeDrawings)
  116.     ON_BN_CLICKED(IDC_ADDBEZIER, OnAddBezier)
  117.     ON_WM_DESTROY()
  118.     ON_BN_CLICKED(IDC_CURVESMOOTH, OnCurveSmooth)
  119.     //}}AFX_MSG_MAP
  120. END_MESSAGE_MAP()
  121.  
  122. /////////////////////////////////////////////////////////////////////////////
  123. // CMakeDwgDlg message handlers
  124.  
  125. // TurboCAD SDK: TurboCAD Class Identifier and Interface Identifier
  126. const CLSID CLSID_Application  = {0x6A481001,0xE531,0x11CF,{0xA1,0x15,0x00,0xA0,0x24,0x15,0x8D,0xAF}};
  127. const CLSID CLSID_XApplication = {0x6A481801,0xE531,0x11CF,{0xA1,0x15,0x00,0xA0,0x24,0x15,0x8D,0xAF}};
  128. const IID IID_IApplication = {0x6A481101,0xE531,0x11CF,{0xA1,0x15,0x00,0xA0,0x24,0x15,0x8D,0xAF}};
  129.  
  130.  
  131. BOOL CMakeDwgDlg::OnInitDialog()
  132. {
  133.     CDialog::OnInitDialog();
  134.  
  135.     // TurboCAD SDK: associate the view with static on dialog box
  136.     m_view.SubclassDlgItem(IDC_PREVIEWRECT, this);
  137.  
  138.     // Add "About..." menu item to system menu.
  139.  
  140.     // IDM_ABOUTBOX must be in the system command range.
  141.     ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
  142.     ASSERT(IDM_ABOUTBOX < 0xF000);
  143.  
  144.     CMenu* pSysMenu = GetSystemMenu(FALSE);
  145.     CString strAboutMenu;
  146.     strAboutMenu.LoadString(IDS_ABOUTBOX);
  147.     if (!strAboutMenu.IsEmpty())
  148.     {
  149.         pSysMenu->AppendMenu(MF_SEPARATOR);
  150.         pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
  151.     }
  152.  
  153.     // Set the icon for this dialog.  The framework does this automatically
  154.     //  when the application's main window is not a dialog
  155.     SetIcon(m_hIcon, TRUE);            // Set big icon
  156.     SetIcon(m_hIcon, FALSE);        // Set small icon
  157.  
  158.     // TODO: Add extra initialization here
  159.  
  160.     return TRUE;  // return TRUE  unless you set the focus to a control
  161. }
  162.  
  163. void CMakeDwgDlg::OnSysCommand(UINT nID, LPARAM lParam)
  164. {
  165.     if ((nID & 0xFFF0) == IDM_ABOUTBOX)
  166.     {
  167.         CAboutDlg dlgAbout;
  168.         dlgAbout.DoModal();
  169.     }
  170.     else
  171.     {
  172.         CDialog::OnSysCommand(nID, lParam);
  173.     }
  174. }
  175.  
  176. // If you add a minimize button to your dialog, you will need the code below
  177. //  to draw the icon.  For MFC applications using the document/view model,
  178. //  this is automatically done for you by the framework.
  179.  
  180. void CMakeDwgDlg::OnPaint()
  181. {
  182.     if (IsIconic())
  183.     {
  184.         CPaintDC dc(this); // device context for painting
  185.  
  186.         SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
  187.  
  188.         // Center icon in client rectangle
  189.         int cxIcon = GetSystemMetrics(SM_CXICON);
  190.         int cyIcon = GetSystemMetrics(SM_CYICON);
  191.         CRect rect;
  192.         GetClientRect(&rect);
  193.         int x = (rect.Width() - cxIcon + 1) / 2;
  194.         int y = (rect.Height() - cyIcon + 1) / 2;
  195.  
  196.         // Draw the icon
  197.         dc.DrawIcon(x, y, m_hIcon);
  198.     }
  199.     else
  200.     {
  201.         CDialog::OnPaint();
  202.     }
  203. }
  204.  
  205. // The system calls this to obtain the cursor to display while the user drags
  206. //  the minimized window.
  207. HCURSOR CMakeDwgDlg::OnQueryDragIcon()
  208. {
  209.     return (HCURSOR) m_hIcon;
  210. }
  211.  
  212. const IID IID_Properties = {0x6A48110F,0xE531,0x11CF,{0xA1,0x15,0x00,0xA0,0x24,0x15,0x8D,0xAF}};
  213.  
  214. // TurboCAD SDK: function to add a circle to the active drawing
  215. void CMakeDwgDlg::OnAddCircle()
  216. {
  217.     // Pointer to Graphics collection
  218.     Graphics* pGraphics = m_view.GetGraphics();
  219.     CString strStr;
  220.     if (pGraphics == NULL)
  221.     {
  222.         strStr.LoadString(IDS_NEEDNEWDRAWINGFIRST);
  223.         AfxMessageBox(strStr);
  224.         return;
  225.     }
  226.  
  227.     // Pointer to graphic object
  228.     IGraphic* pIGraphic = NULL;
  229.     IGraphic* pTGraphic = NULL;
  230.     double xc = 10.0;
  231.     double yc = 10.0;
  232.     double zc = 0.0;
  233.     double xp = 15.0;
  234.     double yp = 10.0;
  235.     double zp = 0.0;
  236.  
  237.     // Add graphic to drawing
  238.     HRESULT hRes = pGraphics->AddCircleCenterAndPoint(xc, yc, zc, xp, yp, zp, &pIGraphic);
  239.     if (SUCCEEDED(hRes))
  240.     {
  241.         // Change pen color of circle
  242.         Properties* pProp = NULL;
  243.         Property* pPenColor = NULL;
  244.  
  245. #if 1
  246.         // Code to find out where the local server bug is...
  247.         IDispatch* pDisp = NULL;
  248.         hRes = pIGraphic->QueryInterface(IID_IDispatch, (void**)&pDisp);
  249.         if (SUCCEEDED(hRes))
  250.         {
  251.             LPOLESTR oszProperties = OLESTR("Properties");
  252.             DISPID dispid;
  253.             hRes = pDisp->GetIDsOfNames(
  254.                 IID_NULL,
  255.                 &oszProperties,
  256.                 1,
  257.                 0,
  258.                 &dispid);
  259.             if (SUCCEEDED(hRes))
  260.             {
  261.                 VARIANTARG args[1];
  262.                 VariantInit(&args[0]);
  263.                 DISPPARAMS params = { args, NULL, 0, 0 };
  264.                 hRes = pDisp->Invoke(dispid,
  265.                     IID_NULL,
  266.                     0,
  267.                     DISPATCH_PROPERTYGET,
  268.                     ¶ms,
  269.                     &args[0],
  270.                     NULL,
  271.                     NULL);
  272.  
  273.                 if (SUCCEEDED(hRes) && args[0].vt == VT_DISPATCH)
  274.                 {
  275.                     // JON: DISP_E_BADVARTYPE returned here!
  276.                     // CGrfPropertiesObject::InternalQueryInterface on the server side IS returning S_OK.
  277.                     // But somewhere the OLE marshaller doesn't like it...
  278.                     hRes = args[0].pdispVal->QueryInterface(IID_Properties, (void**)&pProp);
  279.                 }
  280.                 VariantClear(&args[0]);
  281.             }
  282.  
  283.             pDisp->Release();
  284.         }
  285. #else
  286.         hRes = pIGraphic->get_Properties(&pProp);
  287. #endif
  288.         if (SUCCEEDED(hRes))
  289.         {
  290.             // Change pen color
  291.             COleVariant var("PenColor");
  292.             hRes = pProp->get_Item(&var, &pPenColor);
  293.             if (SUCCEEDED(hRes))
  294.             {
  295.                 var = (long)0x0000FF;
  296.                 hRes = pPenColor->put_Value(0, &var);
  297.  
  298.                 pPenColor->Release();
  299.             }
  300.             pProp->Release();
  301.         }
  302.         CString str("Circle");
  303.         BSTR var = str.AllocSysString();
  304.         hRes = pGraphics->AddText(str.AllocSysString(),10.0, 10.0, 0.0, .5,
  305.                             COleVariant(3.1415/2.0),
  306.                             COleVariant(DISP_E_PARAMNOTFOUND, VT_ERROR),
  307.                             COleVariant(DISP_E_PARAMNOTFOUND, VT_ERROR),
  308.                             COleVariant(DISP_E_PARAMNOTFOUND, VT_ERROR),
  309.                             &pTGraphic);
  310.         // Finished with graphic, decrement the reference count
  311.         if (SUCCEEDED(hRes))
  312.             pTGraphic->Release();
  313.         pIGraphic->Release();
  314.     }
  315.  
  316.     // Finished with Graphics collection, decrement the reference count
  317.     pGraphics->Release();
  318.  
  319.     // Zoom view to extents and invalidate window
  320.     m_view.ZoomAndInvalidate();
  321. }
  322.  
  323. // TurboCAD SDK: function to add a line to the active drawing
  324. void CMakeDwgDlg::OnAddLine()
  325. {
  326.     // Pointer to Graphics collection
  327.     Graphics* pGraphics = m_view.GetGraphics();
  328.     CString strStr;
  329.     if (pGraphics == NULL)
  330.     {
  331.         strStr.LoadString(IDS_NEEDNEWDRAWINGFIRST);
  332.         AfxMessageBox(strStr);
  333.         return;
  334.     }
  335.  
  336.     // Pointer to graphic object
  337.     IGraphic* pIGraphic = NULL;
  338.     double x0 = 5.0;
  339.     double y0 = 10.0;
  340.     double z0 = 0.0;
  341.     double x1 = 15.0;
  342.     double y1 = 10.0;
  343.     double z1 = 0.0;
  344.  
  345.     // Add graphic to drawing
  346.     HRESULT hRes = pGraphics->AddLineSingle(x0, y0, z0, x1, y1, z1, &pIGraphic);
  347.  
  348.     if (SUCCEEDED(hRes))
  349.     {
  350.         // Finished with graphic, decrement reference count
  351.         pIGraphic->Release();
  352.     }
  353.  
  354.     // Finished with the Graphics collection, decrement reference count
  355.     pGraphics->Release();
  356.  
  357.     // Zoom view to extents and invalidate window
  358.     m_view.ZoomAndInvalidate();
  359. }
  360.  
  361. // TurboCAD SDK: function to add a spline to the active drawing
  362. void CMakeDwgDlg::OnAddSpline()
  363. {
  364.     const int NSPLPOINTS = 5;
  365.     static double x[NSPLPOINTS] = { 1.0, 2.0, 5.0, 7.0, 9.0 };
  366.     static double y[NSPLPOINTS] = { 4.0, 6.0, 4.0, 2.0, 5.0 };
  367.     static double z[NSPLPOINTS] = { 0.0, 0.0, 0.0,  0.0, 0.0 };
  368.     CString strStr;
  369.     // Pointer to Graphics collection
  370.     Graphics* pGraphics = m_view.GetGraphics();
  371.     if (pGraphics == NULL)
  372.     {
  373.         strStr.LoadString(IDS_NEEDNEWDRAWINGFIRST);
  374.         AfxMessageBox(strStr);
  375.         return;
  376.     }
  377.  
  378.     // Pointer to graphic object
  379.     IGraphic* pIGraphic = NULL;
  380.  
  381.     // Add graphic to drawing with starting vertex x[0], y[0], z[0]
  382.     HRESULT hRes = pGraphics->AddCurveSpline(x[0], y[0], z[0], &pIGraphic);
  383.     if (SUCCEEDED(hRes))
  384.     {
  385.         Vertices* pVertices = NULL;
  386.         hRes = pIGraphic->get_Vertices(&pVertices);
  387.         if (SUCCEEDED(hRes))
  388.         {
  389.             // Make a "missing" optional parameter
  390.             VARIANT var;
  391.             var.vt = VT_ERROR;
  392.             var.scode = DISP_E_PARAMNOTFOUND;
  393.  
  394.             // Add rest of points to spline curve
  395.             VARIANT varX;
  396.             varX.vt = VT_R8;
  397.             VARIANT varY;
  398.             varY.vt = VT_R8;
  399.             VARIANT varZ;
  400.             varZ.vt = VT_R8;
  401.             int i;
  402.             for (i = 1; i < NSPLPOINTS; ++i)
  403.             {
  404.                 varX.dblVal = x[i];
  405.                 varY.dblVal = y[i];
  406.                 varZ.dblVal = z[i];
  407.                 IVertex* pIVertex = NULL;
  408.                 hRes = pVertices->Add(&varX, &varY, &varZ,
  409.                     &var, &var, &var, &var, &var, &var, &var, &var, &pIVertex);
  410.                 if (pIVertex != NULL)
  411.                     pIVertex->Release();
  412.             }
  413.             pVertices->Release();
  414.  
  415.             // Change pen color of spline
  416.             Properties* pProp = NULL;
  417.             Property* pPenColor = NULL;
  418.  
  419.             hRes = pIGraphic->get_Properties(&pProp);
  420.             if (SUCCEEDED(hRes))
  421.             {
  422.                 // Change pen color
  423.                 COleVariant var("PenColor");
  424.                 hRes = pProp->get_Item(&var, &pPenColor);
  425.                 if (SUCCEEDED(hRes))
  426.                 {
  427.                     var = (long)0x00FF00;
  428.                     hRes = pPenColor->put_Value(0, &var);
  429.  
  430.                     pPenColor->Release();
  431.                 }
  432.                 pProp->Release();
  433.             }
  434.         }
  435.  
  436.         // Finished with graphic, decrement the reference count
  437.         pIGraphic->Release();
  438.     }
  439.  
  440.     // Finished with Graphics collection, decrement reference count
  441.     pGraphics->Release();
  442.  
  443.     // Zoom view to extents and invalidate window
  444.     m_view.ZoomAndInvalidate();
  445. }
  446.  
  447. // TurboCAD SDK: function to add a bezier curve to the active drawing
  448. void CMakeDwgDlg::OnAddBezier()
  449. {
  450.     const int NBEZPOINTS = 7;
  451.     static double x[NBEZPOINTS] = { 1.0, 2.0, 5.0, 7.0, 9.0, 10.0, 11.0 };
  452.     static double y[NBEZPOINTS] = { 4.0, 6.0, 4.0, 2.0, 5.0, 4.0, 2.0 };
  453.     static double z[NBEZPOINTS] = { 0.0, 0.0, 0.0,  0.0, 0.0, 0.0, 0.0 };
  454.     CString strStr;
  455.     // Pointer to Graphics collection
  456.     Graphics* pGraphics = m_view.GetGraphics();
  457.     if (pGraphics == NULL)
  458.     {
  459.         strStr.LoadString(IDS_NEEDNEWDRAWINGFIRST);
  460.         AfxMessageBox(strStr);
  461.         return;
  462.     }
  463.  
  464.     // Pointer to graphic object
  465.     IGraphic* pIGraphic = NULL;
  466.  
  467.     // Add graphic to drawing with starting vertex x[0], y[0], z[0]
  468.     HRESULT hRes = pGraphics->AddCurveBezier(x[0], y[0], z[0], &pIGraphic);
  469.     if (SUCCEEDED(hRes))
  470.     {
  471.         Vertices* pVertices = NULL;
  472.         hRes = pIGraphic->get_Vertices(&pVertices);
  473.         if (SUCCEEDED(hRes))
  474.         {
  475.             // Make a "missing" optional parameter
  476.             VARIANT varMissing;
  477.             varMissing.vt = VT_ERROR;
  478.             varMissing.scode = DISP_E_PARAMNOTFOUND;
  479.  
  480.             // Add rest of points to spline curve
  481.             VARIANT varX;
  482.             varX.vt = VT_R8;
  483.             VARIANT varY;
  484.             varY.vt = VT_R8;
  485.             VARIANT varZ;
  486.             varZ.vt = VT_R8;
  487.             int i;
  488.             for (i = 1; i < NBEZPOINTS; ++i)
  489.             {
  490.                 varX.dblVal = x[i];
  491.                 varY.dblVal = y[i];
  492.                 varZ.dblVal = z[i];
  493.  
  494.                 // Make and set the "Calculated" parameter
  495.                 // On-curve points are not calculated.
  496.                 // Off-curve points are calculated.
  497.                 VARIANT varCalculated;
  498.                 varCalculated.vt = VT_BOOL;
  499.                 varCalculated.boolVal = ((i % 3) != 0) ? -1 : 0;
  500.  
  501.                 IVertex* pIVertex = NULL;
  502.                 hRes = pVertices->Add(&varX, &varY, &varZ,
  503.                     &varMissing, &varMissing, &varMissing, &varMissing, &varMissing,
  504.                     &varCalculated, &varMissing, &varMissing, &pIVertex);
  505.                 if (pIVertex != NULL)
  506.                     pIVertex->Release();
  507.             }
  508.             pVertices->Release();
  509.  
  510.             // Change pen color of spline
  511.             Properties* pProp = NULL;
  512.             Property* pPenColor = NULL;
  513.  
  514.             hRes = pIGraphic->get_Properties(&pProp);
  515.             if (SUCCEEDED(hRes))
  516.             {
  517.                 // Change pen color
  518.                 COleVariant var("PenColor");
  519.                 hRes = pProp->get_Item(&var, &pPenColor);
  520.                 if (SUCCEEDED(hRes))
  521.                 {
  522.                     var = (long)0x00FF00;
  523.                     hRes = pPenColor->put_Value(0, &var);
  524.  
  525.                     pPenColor->Release();
  526.                 }
  527.                 pProp->Release();
  528.             }
  529.         }
  530.  
  531.         // Finished with graphic, decrement the reference count
  532.         pIGraphic->Release();
  533.     }
  534.     // Finished with Graphics collection, decrement reference count
  535.     pGraphics->Release();
  536.  
  537.     // Zoom view to extents and invalidate window
  538.     m_view.ZoomAndInvalidate();
  539. }
  540.  
  541. HRESULT CMakeDwgDlg::SetupView(IDrawing* pIDrawing)
  542. {
  543.     // Pointer to View collection
  544.     Views* pViews = NULL;
  545.  
  546.     // Get the handle to the view collection
  547.     HRESULT hRes = pIDrawing->get_Views(&pViews);
  548.     if (SUCCEEDED(hRes))
  549.     {
  550.         // Pointer to view object
  551.         View* pView = NULL;
  552.  
  553.         // Make a "missing" optional parameter
  554.         VARIANT var;
  555.         var.vt = VT_ERROR;
  556.         var.scode = DISP_E_PARAMNOTFOUND;
  557.  
  558.         // Add the view to the collection
  559.         hRes = pViews->Add(&var, &var, &pView);
  560.  
  561.         if (SUCCEEDED(hRes))
  562.         {
  563.             // Set up view to be the active view
  564.             m_view.SetView(pView);
  565.  
  566.             // Finished with view, decrement reference count
  567.             pView->Release();
  568.         }
  569.  
  570.         // Finished with views, decrement reference count
  571.         pViews->Release();
  572.     }
  573.     return hRes;
  574. }
  575.  
  576.  
  577. void CMakeDwgDlg::EnableButtons()
  578. {
  579.     BOOL bEnable = (m_pIApplication != NULL);
  580.     GetDlgItem(IDC_INPROC_SERVER)->EnableWindow(!bEnable);
  581.     GetDlgItem(IDC_LOCAL_SERVER)->EnableWindow(!bEnable);
  582.     GetDlgItem(IDC_NEWDRAWING)->EnableWindow(bEnable);
  583.     GetDlgItem(IDC_OPENDRAWING)->EnableWindow(bEnable);
  584.  
  585.     if (bEnable)
  586.     {
  587.         bEnable = FALSE;
  588.         Graphics* pGraphics = m_view.GetGraphics();
  589.         if (pGraphics != NULL)
  590.         {
  591.             bEnable = TRUE;
  592.             pGraphics->Release();
  593.         }
  594.     }
  595.     GetDlgItem(IDC_ADDCIRCLE)->EnableWindow(bEnable);
  596.     GetDlgItem(IDC_ADDLINE)->EnableWindow(bEnable);
  597.     GetDlgItem(IDC_ADDSPLINE)->EnableWindow(bEnable);
  598.     GetDlgItem(IDC_ADDBEZIER)->EnableWindow(bEnable);
  599.     GetDlgItem(IDC_SAVEDRAWING)->EnableWindow(bEnable);
  600. }
  601.  
  602. void CMakeDwgDlg::SyncToApplication()
  603. {
  604.     if (m_pIApplication == NULL)
  605.         return;
  606.  
  607.     // Pointer to Drawings collection
  608.     Drawings* pDrawings = NULL;
  609.  
  610.     // Get the handle to the drawings collection
  611.     HRESULT hRes = m_pIApplication->get_Drawings(&pDrawings);
  612.     if (SUCCEEDED(hRes))
  613.     {
  614.         COleVariant varIndex;
  615.         varIndex.vt = VT_I4;
  616.         for (varIndex.lVal = 0L; ; ++varIndex.lVal)
  617.         {
  618.             IDrawing* pIDrawing = NULL;
  619.             hRes = pDrawings->get_Item(&varIndex, &pIDrawing);
  620.             if (FAILED(hRes))
  621.                 break;
  622.             BSTR bstrName = NULL;
  623.             hRes = pIDrawing->get_Name(&bstrName);
  624.             if (SUCCEEDED(hRes))
  625.             {
  626.                 ASSERT(bstrName != NULL);
  627.                 CString strName = bstrName;
  628.                 ::SysFreeString(bstrName);
  629.                 CListBox* pList = (CListBox*)GetDlgItem(IDC_LISTDRAWINGS);
  630.                 int iString = pList->AddString(strName);
  631.                 if (varIndex.lVal == 0)
  632.                 {
  633.                     pList->SetCurSel(iString);
  634.                     SetupView(pIDrawing);
  635.                 }
  636.             }
  637.  
  638.             // Finished with drawing object, decrement reference count
  639.             pIDrawing->Release();
  640.         }
  641.         pDrawings->Release();
  642.     }
  643.  
  644. }
  645.  
  646. void CMakeDwgDlg::OnInprocServer()
  647. {
  648.     CString strStr;
  649.     // TurboCAD SDK: Connect to the server
  650.     HRESULT hRes = CoCreateInstance(
  651.         CLSID_XApplication,    //Class identifier (CLSID) of the object
  652.         NULL,    //Object is or isn't part of an aggregate
  653.         CLSCTX_INPROC_SERVER,    //Context for running executable code
  654.         IID_IApplication,    //Interface identifier
  655.         (void**)&m_pIApplication    //Points to requested interface pointer
  656.     );
  657.  
  658.     if (FAILED(hRes))
  659.     {
  660.         strStr.LoadString(IDS_COULDNOT_CREATE_INPROC_SERVER);
  661.         AfxMessageBox(strStr);
  662.     }
  663.  
  664.     EnableButtons();
  665. }
  666.  
  667. extern "C" const IID IID_IPickRectSink = {0x6A481202,0xE531,0x11CF,{0xA1,0x15,0x00,0xA0,0x24,0x15,0x8D,0xAF}}; 
  668.  
  669. void CMakeDwgDlg::OnLocalServer() 
  670. {
  671.     CString strStr;
  672.     // TurboCAD SDK: Connect to the server
  673.     HRESULT hRes = CoCreateInstance(
  674.         CLSID_Application,    //Class identifier (CLSID) of the object
  675.         NULL,    //Object is or isn't part of an aggregate 
  676.         CLSCTX_LOCAL_SERVER,    //Context for running executable code
  677.         IID_IApplication,    //Interface identifier
  678.         (void**)&m_pIApplication    //Points to requested interface pointer
  679.     );    
  680.  
  681.     if (FAILED(hRes))
  682.     {
  683.         strStr.LoadString(IDS_COULDNOT_CREATE_LOCAL_SERVER);
  684.         AfxMessageBox(strStr);
  685.     }
  686.  
  687.     SyncToApplication();
  688.     EnableButtons();
  689. }
  690.  
  691. void CMakeDwgDlg::OnGetActive() 
  692. {
  693.     
  694.     if (m_pIApplication != NULL)
  695.         return;
  696.  
  697.     CString strStr;
  698.     // Try to connect to running TurboCAD
  699.     IUnknown* pUnk = NULL;
  700.     HRESULT hRes = ::GetActiveObject(CLSID_Application, NULL, &pUnk);
  701.     if (FAILED(hRes))
  702.     {
  703.         strStr.LoadString(IDS_TCAD_ISNOT_ACTIVE);
  704.         AfxMessageBox(strStr);
  705.         return;
  706.     }
  707.     hRes = pUnk->QueryInterface(IID_IApplication, (void**)&m_pIApplication);
  708.     pUnk->Release();
  709.     if (FAILED(hRes))
  710.     {
  711.         strStr.LoadString(IDS_OBJECT_ISNOT_TCADAPP);
  712.         AfxMessageBox(strStr);
  713.     }
  714.  
  715.     SyncToApplication();
  716.     EnableButtons();
  717. }
  718.  
  719. // TurboCAD SDK: function to an active drawing to the application
  720. void CMakeDwgDlg::OnNewDrawing() 
  721. {
  722.     // Pointer to Drawings collection
  723.     Drawings* pDrawings = NULL;
  724.  
  725.     // Get the handle to the drawings collection
  726.     HRESULT hRes = m_pIApplication->get_Drawings(&pDrawings);
  727.     if (FAILED(hRes))
  728.     {
  729.         return;
  730.     }
  731.     
  732.     // Pointer to drawing object
  733.     IDrawing* pIDrawing = NULL;
  734.  
  735.     // Make a "missing" optional parameter
  736.     VARIANT var;
  737.     var.vt = VT_ERROR;
  738.     var.scode = DISP_E_PARAMNOTFOUND;
  739.  
  740.     // Add the new drawing to the drawings collection
  741.     hRes = pDrawings->Add(&var, &pIDrawing);
  742.     if (SUCCEEDED(hRes))
  743.     {
  744.         long lCount = 0;
  745.         hRes = pDrawings->get_Count(&lCount);
  746.  
  747.         char szName[32];
  748.         wsprintf(szName, "Drawing %ld", lCount);
  749.         CListBox* pList = (CListBox*)GetDlgItem(IDC_LISTDRAWINGS);
  750.         pList->SetCurSel(pList->AddString(szName));
  751.  
  752.         hRes = SetupView(pIDrawing);
  753.  
  754.         // Finished with drawing object, decrement reference count
  755.         pIDrawing->Release();
  756.     }
  757.  
  758.     // Finished with drawings collection, decrement reference count
  759.     pDrawings->Release();
  760.  
  761.     EnableButtons();
  762. }
  763.  
  764.  
  765. static char BASED_CODE szFilter[] = "TurboCAD Drawing Files (*.tcw)|*.tcw|All Files (*.*)|*.*||";
  766.   
  767. void CMakeDwgDlg::OnOpenDrawing() 
  768. {
  769.     CFileDialog dlg(TRUE, NULL, NULL, OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT, szFilter, this);
  770.     if (dlg.DoModal() != IDOK)
  771.         return;
  772.  
  773.     // Pointer to Drawings collection
  774.     Drawings* pDrawings = NULL;
  775.  
  776.     // Get the handle to the drawings collection
  777.     HRESULT hRes = m_pIApplication->get_Drawings(&pDrawings);
  778.     if (FAILED(hRes))
  779.     {
  780.         return;
  781.     }
  782.     
  783.     // Pointer to drawing object
  784.     IDrawing* pIDrawing = NULL;
  785.  
  786.     // Make a "missing" optional parameter
  787.     VARIANT var;
  788.     var.vt = VT_ERROR;
  789.     var.scode = DISP_E_PARAMNOTFOUND;
  790.  
  791.     // Get the file name into a BSTR
  792.     CString strPath = dlg.GetPathName();
  793.     BSTR bstrPath = strPath.AllocSysString();
  794.  
  795.     // Add the drawing to the drawings collection
  796.     hRes = pDrawings->Open(bstrPath, &var, &var, &pIDrawing);
  797.  
  798.     // Free the system memory for the BSTR
  799.     ::SysFreeString(bstrPath);
  800.  
  801.     if (SUCCEEDED(hRes))
  802.     {
  803.         CListBox* pList = (CListBox*)GetDlgItem(IDC_LISTDRAWINGS);
  804.         pList->SetCurSel(pList->AddString(strPath));
  805.         
  806.         hRes = SetupView(pIDrawing);
  807.  
  808.         // Finished with drawing object, decrement reference count
  809.         pIDrawing->Release();
  810.     }
  811.     else
  812.     {
  813.         // File was not opened.  Maybe the filter gave us a reason.
  814.         CString strDesc("Unknown");
  815.  
  816.         // Try to get thread-wide error object
  817.         IErrorInfo* pErrorInfo = NULL;
  818.         if (SUCCEEDED(::GetErrorInfo(0, &pErrorInfo)))
  819.         {
  820.             // Try to get description string
  821.             BSTR bstrDesc = NULL;
  822.             if (SUCCEEDED(pErrorInfo->GetDescription(&bstrDesc)))
  823.             {
  824.                 strDesc = bstrDesc;
  825.                 ::SysFreeString(bstrDesc);
  826.             }
  827.             pErrorInfo->Release();
  828.         }
  829.  
  830.         // Display failure message
  831.         CString msg = "Could not open '" + strPath + "': " + strDesc;
  832.         MessageBox(msg);
  833.     }
  834.  
  835.     // Finished with drawings collection, decrement reference count
  836.     pDrawings->Release();
  837.  
  838.     EnableButtons();
  839. }
  840.  
  841. void CMakeDwgDlg::OnSaveDrawing() 
  842. {
  843.     // Pointer to Drawing object
  844.     IDrawing* pIDrawing = m_view.GetDrawing();
  845.     CString strStr;
  846.     if (pIDrawing == NULL)
  847.     {
  848.         strStr.LoadString(IDS_NEEDNEWDRAWINGFIRST);
  849.         AfxMessageBox(strStr);
  850.         return;
  851.     }
  852.     
  853.     CFileDialog dlg(FALSE, NULL, NULL, OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT, szFilter, this);
  854.     if (dlg.DoModal() == IDOK)
  855.     {
  856.         // Make a "missing" optional parameter
  857.         VARIANT var;
  858.         var.vt = VT_ERROR;
  859.         var.scode = DISP_E_PARAMNOTFOUND;
  860.  
  861.         // Get the file name into a BSTR
  862.         CString strPath = dlg.GetPathName();
  863.         BSTR bstrPath = strPath.AllocSysString();
  864.  
  865.         HRESULT hRes = pIDrawing->SaveAs(bstrPath, &var);
  866.  
  867.         // Free the system memory for the BSTR
  868.         ::SysFreeString(bstrPath);
  869.     }
  870.  
  871.     // Done with drawing
  872.     pIDrawing->Release();
  873. }
  874.  
  875. void CMakeDwgDlg::OnSelchangeDrawings() 
  876. {
  877.     if (m_pIApplication == NULL)
  878.         return;
  879.  
  880.     CListBox* pList = (CListBox*)GetDlgItem(IDC_LISTDRAWINGS);
  881.     long l = pList->GetCurSel();
  882.     if (l < 0)
  883.         return;
  884.  
  885.     Drawings* pDrawings = NULL;
  886.     HRESULT hRes = m_pIApplication->get_Drawings(&pDrawings);
  887.     if (FAILED(hRes))
  888.         return;
  889.  
  890.     COleVariant var(l);
  891.     IDrawing* pIDrawing = NULL;
  892.     hRes = pDrawings->get_Item(&var, &pIDrawing);
  893.     if (SUCCEEDED(hRes))
  894.     {
  895.         hRes = pIDrawing->Activate();
  896.         hRes = SetupView(pIDrawing);
  897.  
  898.         // Finished with drawing object, decrement reference count
  899.         pIDrawing->Release();
  900.     }
  901.  
  902.     EnableButtons();
  903. }
  904.  
  905.  
  906. void CMakeDwgDlg::OnDestroy() 
  907. {
  908.     CDialog::OnDestroy();
  909.  
  910.     m_view.SetView(NULL);
  911.  
  912.     // Finished with application, decrement the reference count
  913.     if (m_pIApplication != NULL)
  914.     {
  915.         m_pIApplication->Release();
  916.         m_pIApplication = NULL;
  917.     }
  918. }
  919.  
  920. void CMakeDwgDlg::OnCurveSmooth() 
  921. {
  922.     // Pointer to Graphics collection
  923.     Graphics* pGraphics = m_view.GetGraphics();
  924.     CString strStr;
  925.     if (pGraphics == NULL)
  926.     {
  927.         strStr.LoadString(IDS_NEEDNEWDRAWINGFIRST);
  928.         AfxMessageBox(strStr);
  929.         return;
  930.     }
  931.  
  932.     // Pointer to graphic object
  933.     IGraphic* pIGraphic;
  934.     COleVariant varIndex(0L);
  935.     HRESULT hRes = pGraphics->get_Item(&varIndex, &pIGraphic);
  936.     if (SUCCEEDED(hRes))
  937.     {
  938.         Properties* prop = NULL;
  939.         Property* fprop = NULL;
  940.         if (SUCCEEDED(pIGraphic->get_Properties(&prop)))
  941.         {
  942.             COleVariant vsmooth("$CurveSmooth");
  943.             hRes = prop->get_Item(&vsmooth, &fprop);
  944.             if (SUCCEEDED(hRes))
  945.             {
  946.                 COleVariant varValue;
  947.                 hRes = fprop->get_Value(0, &varValue);
  948.                 if (SUCCEEDED(hRes))
  949.                 {
  950.                     ASSERT(varValue.vt == VT_I2);
  951.                     if (varValue.iVal)
  952.                         varValue.iVal = 0;
  953.                     else
  954.                         varValue.iVal = 1;
  955.                     fprop->put_Value(0, &varValue);
  956.                     m_view.Invalidate();
  957.                 }
  958.                 fprop->Release();
  959.             }
  960.             prop->Release();
  961.         }
  962.         pIGraphic->Release();
  963.     }
  964.     pGraphics->Release();
  965. }
  966.